<?php

class MSanpham extends CI_Model {

    public function __construct() {
        parent::__construct();
        $this->load->database();
        $this->OnCreate();
    }

    private $masp;
    private $tensp;
    private $matl;
    private $machitiet;
    private $giaban;
    private $hinhanh;
    private $mahangsx;
    private $makm;
    private $is_noibat;

    function OnCreate() {
        $this->masp = '';
        $this->tensp = '';
        $this->matl = '';
        $this->machitiet = '';
        $this->giaban = '';
        $this->hinhanh = '';
        $this->mahangsx = '';
        $this->makm = '';
        $this->is_noibat = '';
    }

    public function set_masp($data) {
        $this->masp = $data;
    }

    public function set_tensp($data) {
        $this->tensp = $data;
    }

    public function set_matl($data) {
        $this->matl = $data;
    }

    public function set_machitiet($data) {
        $this->machitiet = $data;
    }

    public function set_giaban($data) {
        $this->giaban = $data;
    }

    public function set_hinhanh($data) {
        $this->hinhanh = $data;
    }

    public function set_mahangsx($data) {
        $this->mahangsx = $data;
    }

    public function set_makm($data) {
        $this->makm = $data;
    }

    public function set_is_noibat($data) {
        $this->is_noibat = $data;
    }

    ////////////////////////
    public function get_masp() {
        return $this->masp;
    }

    public function get_tensp() {
        return $this->tensp;
    }

    public function get_matl() {
        return $this->matl;
    }

    public function get_machitiet() {
        return $this->machitiet;
    }

    public function get_giaban() {
        return $this->giaban;
    }

    public function get_hinhanh() {
        return $this->hinhanh;
    }

    public function get_mahangsx() {
        return $this->mahangsx;
    }

    public function get_makm() {
        return $this->makm;
    }

    public function get_is_noibat() {
        return $this->is_noibat;
    }

    ///////////

    public function GetAllSanpham() {
        return $this->db->get('sanpham')->result_array();
    }

    public function ListAll($number, $offset) {
        if ($offset == FALSE)
            $offset = 1;
        return
                $this->db->order_by('masp')->get('sanpham', $number, ($offset - 1) * ($number))->result_array();
        //echo $this->db->last_query();
    }

    public function GetProductByMatl($number, $offset, $matl) {
        if ($offset == FALSE)
            $offset = 1;
        return
                $this->db->order_by('masp')->where('matl', $matl)->get('sanpham', $number, ($offset - 1) * ($number))->result_array();
        echo $this->db->last_query();
    }

    public function GetProductByMatlMahangsx($number, $offset, $matl, $mahangsx) {
        if ($offset == FALSE)
            $offset = 1;
        $arrSP = $this->db->order_by('masp')->where('matl', $matl)->where('mahangsx', $mahangsx)->get('sanpham', $number, ($offset - 1) * ($number))->result_array();
        return $arrSP;
    }

    public function CountAll() {
        return $this->db->count_all_results('sanpham');
    }

    public function CountAllByFill($matl) {
        return $this->db->where('matl', $matl)->count_all_results('sanpham');
    }

    public function CountAllBy2Fill($matl, $mahangsx) {
        return $this->db->where('matl', $matl)->where('mahangsx', $mahangsx)->count_all_results('sanpham');
    }

    public function CountAllLikeByName($tensp) {
        return $this->db->like('tensp', $tensp)->count_all_results('sanpham');
    }

    //
    public function ListAllByName($tensp) {
        return
                $this->db->order_by('masp')->like("tensp", $tensp)->get('sanpham')->result_array();
        //echo $this->db->last_query();
    }

    public function ListAllByQuery($giaban, $hangsx, $loaisp, $sapxep) {
        $Query = $this->db->query("select * from sanpham " . $giaban . $hangsx . $loaisp . $sapxep);
        return $Query->result_array();
    }

    public function getOject($strWhere) {
        //$this->Oncreate();
        $this->db->from("sanpham");
        $this->db->where($strWhere);
        // $this->db->join('loaitk', 'taikhoan.loaitk=loaitaikhoan.loaitk');
        $query = $this->db->get();
        $ar = $query->row_array();
        if (!empty($ar)) {
            $this->masp = $ar["masp"];
            $this->tensp = $ar["tensp"];
            $this->matl = $ar["matl"];
            $this->machitiet = $ar["machitiet"];
            $this->giaban = $ar["giaban"];
            $this->hinhanh = $ar["hinhanh"];
            $this->mahangsx = $ar["mahangsx"];
            $this->is_noibat = $ar["is_noibat"];
            $this->makm = $ar["makm"];
        }
        return $ar;
    }

    /*
     * 
     * getByID
     * 
     * @masp string
     * @return 1 dong co ID=masp
     * 
     */

    public function getByID($masp) {
        return $this->db->get_where('sanpham', array('masp' => $masp))->row_array();
    }
    /**
     * 
     */
    public function save() {
        $data=array('masp'=>  $this->masp,
            'giaban'=>  $this->giaban,
            'hinhanh'=>  $this->hinhanh,
            'is_noibat'=>  $this->is_noibat,
            'machitiet'=> $this->machitiet,
            'mahangsx'=> $this->mahangsx,
            'makm'=>$this->makm,
            'masp'=>  $this->masp,
            'matl'=>  $this->matl,
            'tensp'=>  $this->tensp);
        return $this->db->insert('sanpham', $data);
       // $this->db->insert();
    }
    public function SearchByIDorName($keyword){
        return $this->db->like('masp',$keyword)->or_like('tensp',$keyword)->get('sanpham')->result_array();
    }
    public function DeleteByID($id){
        return $this->db->where('masp',$id)->delete('sanpham');
      //  $this->db->af
    }
    public function UpdateByID() {       
        $data=array('masp'=>  $this->masp,
            'giaban'=>  $this->giaban,
            'hinhanh'=>  $this->hinhanh,
            'is_noibat'=>  $this->is_noibat,
            'machitiet'=> $this->machitiet,
            'mahangsx'=> $this->mahangsx,
            'makm'=>$this->makm,
            'masp'=>  $this->masp,
            'matl'=>  $this->matl,
            'tensp'=>  $this->tensp);
        $this->db->where('mahangsx', $this->mahangsx);
        return $this->db->update("sanpham", $data);
    }

}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
?>
